package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 品牌表 业务实现类
 *
 * @author atguigu
 * @since 2023-07-25
 */
@Service
public class BaseTrademarkServiceImpl extends ServiceImpl<BaseTrademarkMapper, BaseTrademark> implements BaseTrademarkService {


    @Autowired
    private BaseCategoryTrademarkService baseCategoryTrademarkService;


    /**
     * 分页查询品牌列表
     *
     * @param iPage 分页对象
     * @return
     */
    @Override
    public Page<BaseTrademark> getBaseTrademarkByPage(Page<BaseTrademark> iPage) {
        //1.封装查询条件
        LambdaQueryWrapper<BaseTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(BaseTrademark::getUpdateTime);
        //2.使用入参中分页对象
        //3.执行分页查询-前提提前配置分页拦截器
        return this.page(iPage, queryWrapper);
    }


    /**
     * 根据分类查询该分类下关联品牌列表
     *
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getTrademarkListByCategoryId(Long category3Id) {
        //1.根据分类ID查询分类品牌关系表得到分类品牌集合
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkService.list(queryWrapper);

        //2.从分类品牌集合中获取所有关联到该分类的品牌ID-->集合 List<Long>
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            //2.1 采用Stream流处理集合  将集合类型从BaseCategoryTrademark 转为 Long
            //List<Long> tradeMarkIdList = baseCategoryTrademarkList.stream().map((baseCategoryTrademark) -> {
            //    //获取分类品牌对象中 品牌ID
            //    return baseCategoryTrademark.getTrademarkId();
            //}).collect(Collectors.toList());

            List<Long> tradeMarkIdList = baseCategoryTrademarkList.stream()
                    .map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());

            //3.批量根据品牌主键ID查询品牌集合
            return this.listByIds(tradeMarkIdList);
        }
        return null;
    }

    /**
     * 查询指定分类下可选品牌列表（未关联到当前品牌列表）
     *
     * @param category3Id
     * @return
     */
    @Override
    public List<BaseTrademark> getCurrentTrademarkList(Long category3Id) {
        //获取品牌持久层mapper对象 调用动态SQL完成查询
        return this.baseMapper.getCurrentTrademarkList(category3Id);
    }


    /**
     * 将品牌关联到指定分类
     *
     * @param vo
     * @return
     */
    @Override
    public void saveCategtoryTrademark(CategoryTrademarkVo vo) {
        //1.先获取所有品牌ID
        List<Long> trademarkIdList = vo.getTrademarkIdList();

        //2.将品牌ID集合转为分类品牌集合
        if (!CollectionUtils.isEmpty(trademarkIdList)) {
            List<BaseCategoryTrademark> collect = trademarkIdList.stream().map(tradeMarkId -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setCategory3Id(vo.getCategory3Id());
                baseCategoryTrademark.setTrademarkId(tradeMarkId);
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            //3.执行批量保存
            baseCategoryTrademarkService.saveBatch(collect);
        }
    }

    /**
     * 解除分类关联品牌
     *
     * @param category3Id
     * @param trademarkId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeCategtoryTrademark(Long category3Id, Long trademarkId) {
        //1.构建删除条件对象
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        queryWrapper.eq(BaseCategoryTrademark::getTrademarkId, trademarkId);
        //2.执行删除操作-逻辑
        baseCategoryTrademarkService.remove(queryWrapper);
    }
}
